#include"MessageBox.h"
#include "models/command.h"
#include <QHBoxLayout>
#include <QPainter>

CustomMessageBox::CustomMessageBox(QWidget *parent):
    QMessageBox(parent)
{
    // 创建半透明黑色背景窗口
    QWidget* maskWidget = new QWidget(parent);
    maskWidget->setStyleSheet("background-color: rgba(0, 0, 0, 40%);");
    if(parent)
        maskWidget->setGeometry(0,0,parent->geometry().width(),parent->geometry().height());
    maskWidget->show();
    // 当对话框关闭时删除遮罩层
    connect(this, &QDialog::finished, maskWidget, &QWidget::deleteLater);
    setWindowFlags(windowFlags()|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setMinimumSize(300*g_xScale,150*g_yScale);
    setMaximumSize(600*g_xScale,500*g_yScale);
    setStyleSheet(QString("\
    #qt_msgboxex_icon_label{ \
        margin:%5px 0px 0px %5px;\
                  }\
    #qt_msgbox_label{ \
        color:lightgray; \
        font: %3px \"微软雅黑\"; \
        margin :%1px %1px %2px 0px;\
        min-width: %4px;\
        background-color:transparent; \
    } \
    QMessageBox QPushButton { \
        background-color: #007BFF; \
        color: white; \
        border-radius: 5px; \
        padding: %2px %1px; \
        margin :0px %1px %1px 0px;\
        font: %3px \"微软雅黑\"; \
    } \
    QMessageBox QPushButton:pressed { \
        background-color: #0056b3; \
    }").arg(int(30*g_xScale)).arg(int(10*g_xScale)).arg(int(18*g_xScale)).arg(int(500*g_xScale)).arg(int(26*g_xScale)));

}

void CustomMessageBox::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 绘制背景图片
    QPixmap pixmap(":/images/popup_background.png");
    int nRadius = 30;
    int width = this->width();
    int height = this->height();

    // 绘制左上角
    painter.drawPixmap(0, 0, nRadius, nRadius, pixmap, 0, 0, nRadius, nRadius);
    // 绘制右上角
    painter.drawPixmap(width-nRadius, 0, nRadius, nRadius, pixmap, pixmap.width()-nRadius, 0, nRadius, nRadius);
    // 绘制左下角
    painter.drawPixmap(0, height-nRadius, nRadius, nRadius, pixmap, 0, pixmap.height()-nRadius, nRadius, nRadius);
    // 绘制右下角
    painter.drawPixmap(width-nRadius, height-nRadius, nRadius, nRadius, pixmap, pixmap.width()-nRadius, pixmap.height()-nRadius, nRadius, nRadius);

    // 绘制上边
    painter.drawPixmap(nRadius, 0, width-2*nRadius, nRadius, pixmap, nRadius, 0, pixmap.width()-2*nRadius, nRadius);
    // 绘制下边
    painter.drawPixmap(nRadius, height-nRadius, width-2*nRadius, nRadius, pixmap, nRadius, pixmap.height()-nRadius, pixmap.width()-2*nRadius, nRadius);
    // 绘制左边
    painter.drawPixmap(0, nRadius, nRadius, height-2*nRadius, pixmap, 0, nRadius, nRadius, pixmap.height()-2*nRadius);
    // 绘制右边
    painter.drawPixmap(width-nRadius, nRadius, nRadius, height-2*nRadius, pixmap, pixmap.width()-nRadius, nRadius, nRadius, pixmap.height()-2*nRadius);

    // 绘制中间
    painter.drawPixmap(nRadius, nRadius, width-2*nRadius, height-2*nRadius, pixmap, nRadius, nRadius, pixmap.width()-2*nRadius, pixmap.height()-2*nRadius);

    // 绘制文本和其他内容
    QMessageBox::paintEvent(event);
}


int question(QWidget* pParent, const QString& text,
             QString btn0Text ,
             QString btn1Text ,
             QString btn2Text )
{
    CustomMessageBox msgBox(pParent);

    msgBox.setIcon(QMessageBox::Question);
    msgBox.setText(text);
    msgBox.addButton(btn0Text,QMessageBox::NoRole);
    msgBox.addButton(btn1Text,QMessageBox::NoRole);
    if(!btn2Text.isEmpty())
        msgBox.addButton(btn2Text,QMessageBox::NoRole);
    return msgBox.exec();
}


int warning(QWidget* pParent,const QString& text)
{
    CustomMessageBox msgBox(pParent);
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setText(text);
    msgBox.addButton(QObject::tr("确定"),QMessageBox::AcceptRole);
    return msgBox.exec();
}

int information(QWidget* pParent,const QString& text)
{
    CustomMessageBox msgBox(pParent);
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setText(text);
    msgBox.addButton(QObject::tr("确定"),QMessageBox::AcceptRole);
    return msgBox.exec();
}
